﻿using System.Windows.Forms;

namespace SingleInstanceApplication.WinFormsApp
{
	public partial class MainForm : Form
	{
		/// <summary>
		/// Gets Previouse Window State
		/// </summary>
		public FormWindowState PreviouseWindowState { get; private set; }

		public MainForm()
		{
			InitializeComponent();

			// store 1st value
			PreviouseWindowState = WindowState;
		}

		/// <summary>
		/// Raises the <see cref="E:Layout"/> event.
		/// </summary>
		/// <param name="levent">The <see cref="System.Windows.Forms.LayoutEventArgs"/> instance containing the event data.</param>
		protected override void OnLayout(LayoutEventArgs levent)
		{
			base.OnLayout(levent);
			PreviouseWindowState = WindowState;
		}

		/// <summary>
		/// Activates Window
		/// </summary>
		/// <param name="restoreIfMinimized">if [true] restore prev. win. state</param>
		public void Activate(bool restoreIfMinimized)
		{
			if (restoreIfMinimized && WindowState == WindowState)
			{
				WindowState = PreviouseWindowState == FormWindowState.Normal
										? FormWindowState.Normal : FormWindowState.Maximized;
			}
			Activate();
		}

		/// <summary>
		/// Apends the args.
		/// </summary>
		/// <param name="args">The args.</param>
		public void ApendArgs(string[] args)
		{
			if (args == null) return;
			txtArgs.AppendText(string.Format("[{0}] Received Args:\r\n", System.DateTime.Now));
			txtArgs.AppendText(string.Join("\r\n", args).Trim());
			txtArgs.AppendText("\r\n===============================");
		}
	}
}